﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace SoftwareLogistics.SharepointClient.Library.Models
{
    public class SPJoin
    {
        XElement _root;

        public SPJoin(string list1, string list2, string pkey)
        {
            _root = new XElement("Join",
                new XAttribute("Type", "LEFT"),
                new XAttribute("ListAlias", list2));

            var field1Element = new XElement("FieldRef",
                    new XAttribute("List", list1),
                    new XAttribute("Name", pkey),
                    new XAttribute("RefType", "Id"));

            var field2Element = new XElement("FieldRef",
                    new XAttribute("List", list2),
                    new XAttribute("Name", "ID"));

            _root.Add(new XElement("Eq", field1Element, field2Element));  
        }

        public SPJoin(string list2, string pkey)
        {
            _root = new XElement("Join",
                new XAttribute("Type", "LEFT"),
                new XAttribute("ListAlias", list2));

            var field1Element = new XElement("FieldRef", 
                    new XAttribute("Name", pkey),
                    new XAttribute("RefType", "Id"));

            var field2Element = new XElement("FieldRef", 
                    new XAttribute("List", list2),
                    new XAttribute("Name", "ID"));

            _root.Add(new XElement("Eq", field1Element, field2Element));  
        }


        internal XElement GetJoinElement()
        {
            return _root;
        }
    }
}
